import type { IDicts } from '../types'

export function useDicts() {
    const EnterpriseRank: IDicts[] = [ // 企业级别
        {
            label: '一级企业',
            value: '1',
        },
        {
            label: '二级企业',
            value: '2',
        },
    ]

    const EnterpriseStatus: IDicts[] = [ // 企业状态
        {
            label: '启用',
            value: '0',
        },
        {
            label: '禁用',
            value: '1',
        },
    ]
    const CooperationType: IDicts[] = [ // 合作类型
        {
            label: '入驻企业',
            value: '1',
        },
        {
            label: '三方对接',
            value: '2',
        },
        {
            label: '合作推广',
            value: '3',
        },
    ]

    // TODO: 房交会用户管理共用
    const dicts = {
        CooperationType,
        EnterpriseRank,
        EnterpriseStatus,
    }

    const useDict = (key: string, value: any, getLabel = true, source?: any) => {
        const data = source || dicts
        if (Array.isArray(value)) {
            const labels: any = []
            value.forEach((val: any) => {
                let tgt = data[key].find((v: any) => v.value === `${val}`)?.label || val
                if (!getLabel)
                    tgt = data[key].find((v: any) => v.label === `${val}`)?.value || val

                labels.push(tgt)
            })
            return labels.join(',')
        }
        let target = data[key].find((v: any) => v.value === `${value}`)?.label || value
        if (!getLabel)
            target = data[key].find((v: any) => v.label === `${value}`)?.value || value

        return target
    }

    return { dicts, useDict }
}
